<template>
  <div id="spaceAI_app">
    <!-- <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/> -->
    <router-view />
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
// import { initDialog } from '@/utils'
import { connectWallet } from "@/api/index";
import Web3 from 'web3';
import { getUserInfo } from './request/personalCenter';
import { setResponseCallback } from './request';
import AuthUtils from './utils/auth';

export default {
  name: 'App',
  components: {
    // HelloWorld
  },
  mounted(){
    // initDialog()
    // this.getAdd()
    this.setIncomingInviteCode();

    this.initGlobalResponseCallback();

    if(AuthUtils.isLogin()){
      getUserInfo();
    }
  },
  methods:{
    initGlobalResponseCallback() {
      setResponseCallback('/spaceai/getUserinfo', (response) => {
        if(response.code == 1){
          this.$store.dispatch('userStore/setGoogleAuthenticator', response.data.is_bind_google == 1);
          this.$store.dispatch('userStore/setHasPayPassword', response.data.is_pay_password == 1);
          this.$store.dispatch('userStore/setCanWithdraw', response.data.is_effective == 1);
        }
      });
    },


    setIncomingInviteCode() {
      const urlParams = new URLSearchParams(window.location.search);
      let inviteAddress = urlParams.get('invite_code');

      if(!inviteAddress){
        const hash = window.location.hash;
        const queryStr = hash.includes('?') ? hash.split('?')[1] : '';
        const urlParams = new URLSearchParams(queryStr);
        inviteAddress = urlParams.get('invite_code');
      }

      if(inviteAddress){
        localStorage.setItem('invite_code', inviteAddress);
      }else{
        localStorage.removeItem('invite_code');
      }
    },

    //监听钱包切换账号，从而退出登录
    async getAdd(){
      let add = localStorage.getItem('address') || ''
      const web3 = new Web3(window.ethereum);
        // 监听地址切换事件
        window.ethereum.on('accountsChanged', (accounts) => {
          // 弹出确认弹窗
          if (accounts[0] && accounts[0] != add) {
            this.$toast.fail(this.$t("登录"))
            console.log(accounts[0] , '用户确认切换地址');
            localStorage.removeItem('address')
            localStorage.removeItem('token')
            this.$router.push('/login')
          }
        })
      const res = await connectWallet('app')
      if(!!res.useAccount){
        // 返回钱包地址
        // this.walletAddress = res.useAccount
        if(res.useAccount != add){
          this.$toast.fail(this.$t("登录"))
          localStorage.removeItem('address')
          localStorage.removeItem('token')
          this.$router.push('/login')
        }
      }else{
        this.$toast.fail(this.$t("钱包验证失败"))
      }
    }
  },
  watch: {
    '$i18n.locale'(newVal) {
        // 可以在这里处理一些全局的语言相关逻辑
        this.$forceUpdate() // 强制更新组件树
    }
  }
}
</script>

<style lang="less">
@import "~@/assets/styles/vant-cover.less";
#spaceAI_app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: @while;
  width: 100%;
  min-height: 100vh;
  background: @box-background-color;
}
</style>
